/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import com.velocityme.enums.ChangeDeltaItemType;
import com.velocityme.enums.ChangeDeltaType;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQueries({
    @NamedQuery(name="findChangeDeltaByType",
                query="SELECT DISTINCT c FROM ChangeDelta c WHERE c.node = :node AND c.changeDeltaType = :changeDeltaType"),
    @NamedQuery(name="findChangeDeltaByNode",
                query="SELECT c FROM ChangeDelta c WHERE c.node = :node ORDER BY c.creationTime")
})
public class ChangeDelta implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date creationTime;

    private ChangeDeltaType changeDeltaType;

    @ManyToOne
    private User user;

    @ManyToOne
    private Node node;

    @OneToOne
    private FileAttachment fileAttachment;

    @OneToMany(mappedBy = "changeDelta", cascade = CascadeType.ALL)
    private List<ChangeDeltaItem> changeDeltaItems = new ArrayList<ChangeDeltaItem>();

    protected ChangeDelta() {
    }
    
    public ChangeDelta(ChangeDeltaType changeDeltaType, User user, Node node) {
        this.creationTime = new Date();
        this.changeDeltaType = changeDeltaType;
        this.user = user;
        this.node = node;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public FileAttachment getFileAttachment() {
        return fileAttachment;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public User getUser() {
        return user;
    }

    public ChangeDeltaType getChangeDeltaType() {
        return changeDeltaType;
    }

    public List<ChangeDeltaItem> getChangeDeltaItems() {
        return changeDeltaItems;
    }

    public void addItem(ChangeDeltaItemType changeDeltaItemType, String description) {
        ChangeDeltaItem changeDeltaItem = new ChangeDeltaItem(this, changeDeltaItemType, description);
        changeDeltaItems.add(changeDeltaItem);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ChangeDelta)) {
            return false;
        }
        ChangeDelta other = (ChangeDelta) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.velocityme.entity.ChangeDelta[id=" + id + "]";
    }

}
